function createXMLHttpRequest()
{
	var request = null;
	try
	{
		request = new XMLHttpRequest();
	}
	catch(trymicrosoft)
	{
		try 
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed)
			{
				//alert();
			}
		}
	}

	return request;
}

var g_vRequests = new Array();

function Request(httpMethod, isAsync)
{
	this.request = createXMLHttpRequest();
	if (null == this.request)
		throw "unsupported browser, use IE6.0 or higher to resolve this problem";
	
	g_vRequests[g_vRequests.length] = this;
	this.requestId = g_vRequests.length - 1;
	
	this.httpMethod = (undefined == httpMethod ? "GET" : httpMethod);
	this.isAsynchronous = (undefined == isAsync ? true : isAsync);
	//this.request.reqeustObject = this;
	
	this.send = function(url, sendData)
	{
		this.request.open(this.httpMethod, url, this.isAsync);
		this.request.onreadystatechange = this.onReadyStateChange;
		
		if ("POST" == this.httpMethod.toUpperCase())
		{
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			if (undefined == sendData)
				sendData = null;
			if (null != sendData && undefined != sendData.tagName && "form" == sendData.tagName.toLowerCase())
				sendData = generateQueryString(sendData);
			this.request.send(sendData);
		}
		else
		{
			this.request.send(null);
		}
	};
	
	var code = 
	"this.onReadyStateChange = function()\n" + 
	"{\n" + 
	"	var req = g_vRequests[" + this.requestId + "];\n" + 
	"	//alert(req.request.readyState); \nswitch(req.request.readyState)\n" + 
	"	{\n" + 
	"		case 0: // before open be called\n" + 
	"		case 1: // request ready to send\n" + 
	"		case 2: // sending request\n" + 
	"		case 3: // response begins\n" + 
	"			break;" + 
	"		case 4: // response ready\n" + 
	"			//alert(\"SC: \" + req.request.status); \nif (200 == req.request.status)\n" + 
	"			{//alert(req.request.responseText);\n" + 
	"				if (undefined != req.onCorrectResponse)\n" + 
	"				{\n" + 
	"					req.onCorrectResponse(req.request.responseXML);\n" + 
	"				}\n" + 
	"				else\n" + 
	"				{\n" + 
	"					alert(\"HTTP Status Code: \" + req.request.status);\n" + 
	"				}\n" + 
	"			}\n" + 
	"			else\n" + 
	"			{\n" + 
	"				throw \"HTTP Status Error Code: \" + req.request.status;\n" + 
	"			}\n" + 
	"			break;\n" + 
	"	}\n" + 
	"};";
	//alert(code);
	eval(code);
}
function generateQueryString(f)
{
	var qryStr = "";
	var i;
	for (i = 0; undefined != f.elements && i < f.elements.length; ++i)
	{
		var obj = f.elements[i];
		
		if (undefined != obj.name && null != obj.name && "" != obj.name)
		{
			if ("input" == obj.tagName.toLowerCase())
			{
				if ("checkbox" != obj.type && "radio" != obj.type)
				{
					if (0 < i)
						qryStr += "&";
					
					qryStr += obj.name + "=" + encodeURIComponent(obj.value);
				}
				else if (obj.checked)
				{
					if (0 < i)
						qryStr += "&";
					
					qryStr += obj.name + "=" + encodeURIComponent(obj.value);
				}
			}
			else
			{
				if (0 < i)
					qryStr += "&";
				
				qryStr += obj.name + "=" + encodeURIComponent(obj.innerText);
			}
		}
	}
	
	return qryStr;
}
